<?php
declare (strict_types = 1);

namespace app\middleware;


use think\Exception;

class auth
{
    protected $auth;

    public function __construct(\thans\jwt\JWTAuth $auth)
    {
        $this->auth = $auth;
    }

    public function handle(\think\Request $request, \Closure $next)
    {
        //
        $token = $this->auth->getToken();
        if(!$token){
            return json(['msg'=>'token必传'],403);
        }

        try{
            if($this->auth->validate($token)){
                return json(['msg'=>'token错误,或者token在黑名单中'],403);
            }
        }catch (\Exception $e){
            return json(['msg'=>'token不合法'],403);
        }

        try{
            $arr = $this->auth->getPayload();
            $request->tokenInfo = $arr;
            $request->token = $token;
        }catch (\Exception $e){
            return json(['msg'=>'token过期'],403);
        }

        return $next($request);
    }
}
